package com.the4man.cnbdw.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.service.LevelRoomTypeMapService;
import com.the4man.cnbdw.service.LogInfoService;
import com.the4man.cnbdw.service.RoomTypeService;
import com.the4man.cnbdw.utils.Page;
import com.the4man.cnbdw.utils.SelectModel;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.*;
import com.the4man.framework.model.gen.RoomTypeExampleBase;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.LOG;
import com.the4man.framework.util.PageJson;
import com.the4man.framework.utils.RequestUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Action(
        value = "roomTypeAction",
        results = {@Result(
                name = "addPage",
                location = "/cnbdw/roomType/add.jsp"
        ), @Result(
                name = "listPage",
                location = "/cnbdw/roomType/list.jsp"
        ), @Result(
                name = "editPage",
                location = "/cnbdw/roomType/edit.jsp"
        )}
)
public class RoomTypeAction extends BaseAction implements ModelDriven<RoomType> {
    private RoomType roomType = new RoomType();
    private LogInfo logInfo = new LogInfo();
    @Autowired
    private LogInfoService logService;
    @Autowired
    private RoomTypeService roomTypeService;
    @Autowired
    private LevelRoomTypeMapService levelRoomTypeMapService;

    public RoomTypeAction() {
    }

    public String indexPage() {
        return "listPage";
    }

    public String toAddPage() {
        return "addPage";
    }

    public String toEditPage() {
        try {
            RoomType d = (RoomType)this.roomTypeService.selectByPrimaryKey(this.getRequest().getParameter("id"), RoomType.class);
            BeanUtils.copyProperties(d, this.roomType);
        } catch (Exception var2) {
            var2.printStackTrace();
        }

        return "editPage";
    }

    public void getAllDatasourceList() {
        try {
            RoomTypeExample roomTypeExample = new RoomTypeExample();
            RoomTypeExampleBase.Criteria cc = roomTypeExample.createCriteria();
            if(StringUtils.isNotEmpty(this.roomType.getName())) {
                cc.andNameLike("%" + this.roomType.getName() + "%");
            }

            if(this.roomType.getType() != null) {
                cc.andTypeEqualTo(this.roomType.getType());
            }

            this.roomTypeService.countByExample(roomTypeExample);
            Page<RoomType> page = new Page();
            this.setPageProperties(page);
            this.roomTypeService.selectByExample(roomTypeExample, page);
            DataGrid d = new DataGrid();
            d.setRows(page.getData());
            d.setTotal(Long.valueOf((long)page.getRowCount()));
            this.writeJson(d);
        } catch (Exception var6) {
            LOG.error("获取数据失败", var6, new String[0]);
        }

    }

    public void getRoomTypeRelation() {
        DataGrid d = new DataGrid();

        try {
            LevelRoomTypeMapExample levelRoomTypeMapExample = new LevelRoomTypeMapExample();
            levelRoomTypeMapExample.createCriteria().andLidEqualTo(this.getRequest().getParameter("levelId"));
            List<LevelRoomTypeMap> list = this.levelRoomTypeMapService.selectByExample(levelRoomTypeMapExample);
            if(list != null && !list.isEmpty()) {
                List<String> roomTypeIds = new ArrayList();
                Iterator var6 = list.iterator();

                while(var6.hasNext()) {
                    LevelRoomTypeMap levelRoomTypeMap = (LevelRoomTypeMap)var6.next();
                    roomTypeIds.add(levelRoomTypeMap.getRtid());
                }

                RoomTypeExample example = new RoomTypeExample();
                example.createCriteria().andIdIn(roomTypeIds);
                List roomTypes = this.roomTypeService.selectByExample(example);
                d.setRows(roomTypes);
                d.setTotal(Long.valueOf((long)roomTypes.size()));
            }
        } catch (Exception var7) {
            LOG.error("获取数据失败", var7, new String[0]);
        }

        this.writeJson(d);
    }

    public void add() {
        PageJson json = new PageJson();

        try {
            String name = this.getRequest().getParameter("name");
            RoomType rt = this.roomTypeService.selectByName(name);
            if(rt == null) {
                String time = DateUtil.getInstance().formatDate(new Date());
                this.roomType.setId(UUID.randomUUID().toString());
                this.roomType.setCreateTime(time);
                this.roomType.setUpdateTime(time);
                this.roomType.setCreateUser(RequestUtil.getSessionUserinfo().getUsername());
                this.roomType.setUpdateUser(RequestUtil.getSessionUserinfo().getUsername());
                this.roomTypeService.insert(this.roomType);
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(time);
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("舱室类型管理");
                this.logInfo.setOp("舱室类型添加");
                this.logInfo.setMemo("舱室类型:" + name);
                this.logService.insert(this.logInfo);
                json.setReturnFlag(true);
                json.setReturnMsg("提交成功！");
            } else {
                json.setReturnFlag(false);
                json.setReturnMsg("舱室名已存在");
            }
        } catch (Exception var5) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var5, new String[0]);
        }

        this.writeJson(json);
    }

    public void update() {
        PageJson json = new PageJson();

        try {
            String time = DateUtil.getInstance().formatDate(new Date());
            String name = this.roomType.getName();
            String id = this.roomType.getId();
            RoomType rt = this.roomTypeService.selectMByName(id, name);
            if(rt == null) {
                this.roomType.setUpdateTime(time);
                this.roomType.setUpdateUser(RequestUtil.getSessionUserinfo().getUsername());
                this.roomTypeService.updateByPrimaryKey(this.roomType);
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(time);
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("舱室类型管理");
                this.logInfo.setOp("舱室类型修改");
                this.logInfo.setMemo("舱室类型:" + this.roomType.getName());
                this.logService.insert(this.logInfo);
                json.setReturnFlag(true);
                json.setReturnMsg("提交成功！");
            } else {
                json.setReturnFlag(false);
                json.setReturnMsg("舱室名已存在");
            }
        } catch (Exception var6) {
            json.setReturnFlag(false);
            json.setReturnMsg("提交出错了");
            LOG.error("获取数据失败", var6, new String[0]);
        }

        this.writeJson(json);
    }

    public String executeModifyResult() {
        try {
            Object instance = this.roomTypeService.selectByPrimaryKey(this.roomType.getId(), RoomType.class);
            this.getRequest().setAttribute("instance", instance);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

        return "modifyPage";
    }

    public void delete() {
        PageJson json = new PageJson();

        try {
            int count = 0;
            String ids = this.getRequest().getParameter("ids");
            if(ids.contains(",")) {
                String[] str = ids.split(",");
                String[] var8 = str;
                int var7 = str.length;

                for(int var6 = 0; var6 < var7; ++var6) {
                    String info = var8[var6];
                    RoomType d = (RoomType)this.roomTypeService.selectByPrimaryKey(info, RoomType.class);
                    int i = this.roomTypeService.deleteByPrimaryKey(info, RoomType.class);
                    if(i > 0) {
                        this.logInfo.setId(UUID.randomUUID().toString());
                        this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                        this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                        this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                        this.logInfo.setFunc("舱室类型管理");
                        this.logInfo.setOp("舱室类型批量删除");
                        this.logInfo.setMemo("舱室类型:" + d.getName());
                        this.logService.insert(this.logInfo);
                        ++count;
                    }
                }
            } else {
                RoomType d = (RoomType)this.roomTypeService.selectByPrimaryKey(this.getRequest().getParameter("ids"), RoomType.class);
                count = this.roomTypeService.deleteByPrimaryKey(this.getRequest().getParameter("ids"), RoomType.class);
                this.logInfo.setId(UUID.randomUUID().toString());
                this.logInfo.setTime(DateUtil.getInstance().formatDate(new Date()));
                this.logInfo.setUserName(RequestUtil.getSessionUserinfo().getUsername());
                this.logInfo.setUserId(RequestUtil.getSessionUserinfo().getUserid());
                this.logInfo.setFunc("舱室类型管理");
                this.logInfo.setOp("舱室类型删除");
                this.logInfo.setMemo("舱室类型:" + d.getName());
                this.logService.insert(this.logInfo);
            }

            if(count > 0) {
                json.setReturnMsg("成功删除" + count + "个舱室类型！");
                json.setReturnFlag(true);
            } else {
                json.setReturnMsg("删除舱室类型失败！");
                json.setReturnFlag(false);
            }
        } catch (Exception var11) {
            LOG.error("批量删除舱室类型失败！", var11, new String[0]);
            json.setReturnMsg("删除舱室类型失败！");
            json.setReturnFlag(false);
        }

        this.writeJson(json);
    }

    public void verify() {
        PageJson json = new PageJson();
        this.writeJson(json);
    }

    public void getTypeSelectData() {
        try {
            List<SelectModel> list = this.roomTypeService.getRoomTypeSelectData();
            this.writeJson(list);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

    }

    public void getTypeValue() {
        try {
            RoomType d = (RoomType)this.roomTypeService.selectByPrimaryKey(this.getRequest().getParameter("id"), RoomType.class);
            this.writeJson(d);
        } catch (Exception var2) {
            LOG.error("获取数据失败", var2, new String[0]);
        }

    }

    public RoomType getModel() {
        return this.roomType;
    }
}
